﻿using System.Windows;
using System.Windows.Controls;

namespace ChessKit.Ui
{
  public class EdgeControl : ContentControl
  {
    #region ' TranslationMap Property '

    public string TranslationMap
    {
      get { return (string)GetValue(TranslationMapProperty); }
      set { SetValue(TranslationMapProperty, value); }
    }

    public static readonly DependencyProperty TranslationMapProperty =
      DependencyProperty.Register("TranslationMap", typeof(string), 
      typeof(EdgeControl), new PropertyMetadata(null));


    #endregion

    #region ' Value Property '

    public int Value
    {
      get { return (int)GetValue(ValueProperty); }
      set { SetValue(ValueProperty, value); }
    }

    public static readonly DependencyProperty ValueProperty =
      DependencyProperty.Register("Value", typeof(int), typeof(EdgeControl),
                                  new PropertyMetadata(0));

    #endregion

    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();

      if (Content == null) Content = Translate(Value);
    }

    private string Translate(int value)
    {
      if (TranslationMap == null) return value.ToString();
      var index = value - 1;
      if (index < 0 || index >= TranslationMap.Length) return value.ToString();
      return TranslationMap[index].ToString();
    }
  }
}